package com.refusea.train.task.impl

import com.refusea.train.domain.entity.Referrer
import com.refusea.train.domain.entity.ReferrerCommission
import com.refusea.train.domain.entity.ReferrerCommission.Cat.ORDER
import com.refusea.train.domain.entity.ReferrerCommission.Cat.SIGNUP
import com.refusea.train.mapper.OrderMapper
import com.refusea.train.mapper.ReferrerCommissionMapper
import com.refusea.train.mapper.ReferrerMapper
import com.refusea.train.mapper.UserMapper
import com.refusea.train.task.ReferrerCommissionTask
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.time.LocalDate

/**
 * @author Zhang Yan 2024/4/25
 **/
@Component
class ReferrerCommissionTaskImpl(
    private val userMapper: UserMapper,
    private val orderMapper: OrderMapper,
    private val referrerMapper: ReferrerMapper,
    private val referrerCommissionMapper: ReferrerCommissionMapper,
) : ReferrerCommissionTask {


    @Scheduled(cron = "0 0 1 * *?")
    override fun settle() {
        val today = LocalDate.now()
        referrerMapper.listAll().forEach {
            settleSignup(it, today)
            settleOrder(it, today)
        }
    }


    private fun settleSignup(referrer: Referrer, today: LocalDate) {

        if (referrer.signupCommission <= 0) {
            return
        }

        val maxOt = referrerCommissionMapper.maxOt(referrer.id!!, SIGNUP)

        // list all users whose referrer is the specified referrer and sign up after the last time
        val users = userMapper.listByReferrer(referrer.id!!, maxOt, today)
        val commissions = users.map {
            ReferrerCommission(
                0,
                referrer.schoolId!!,
                referrer.id!!,
                SIGNUP,
                it.id,
                0,
                referrer.signupCommission,
                false,
                it.ct!!,
                null
            )
        }
        referrerCommissionMapper.inserts(commissions)
    }


    private fun settleOrder(referrer: Referrer, today: LocalDate) {

        if (referrer.orderCommission <= 0) {
            return
        }

        val users = userMapper.listAllByReferrer(referrer.id!!)
        if (users.isEmpty()) {
            return
        }

        val maxOt = referrerCommissionMapper.maxOt(referrer.id!!, ORDER)

        // list all orders whose parent is the specified referrer and paid after the last time
        val orders = orderMapper.listByUsers(users.map { it.id }, maxOt, today)
        val commissions = orders.map {
            ReferrerCommission(
                0,
                referrer.schoolId!!,
                referrer.id!!,
                ORDER,
                it.parentId,
                it.id,
                referrer.orderCommission.coerceAtMost(100) * it.amount / 100,
                false,
                it.pt!!,
                null
            )
        }
        referrerCommissionMapper.inserts(commissions)
    }
}
